package com.song.controller;

import com.song.pojo.Result;
import com.song.pojo.Student;
import com.song.pojo.StudentQueryParam;
import com.song.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping
    public Result getStudents(StudentQueryParam  param) {
        return Result.success(studentService.getStudents(param));
    }
    //添加学生信息
    @PostMapping
    public Result addStudent(@RequestBody Student student) {
        log.info("添加学生信息，参数：{}", student);
       return studentService.addStudent(student) !=0 ? Result.success() : Result.error("添加失败");
    }
    //批量删除学生信息
    @DeleteMapping("/{ids}")
    public Result deleteStudent(@PathVariable List<Integer> ids) {
        log.info("删除学生信息，参数：{}", ids);
        if(ids == null || ids.isEmpty()) return Result.error("ids不能为空");
        return studentService.deleteStudent(ids) !=0 ? Result.success() : Result.error("删除失败");
    }
    //根据id查询学生信息
    @GetMapping("/{id}")
    public Result getStudent(@PathVariable Integer id) {
        log.info("根据id查询学生信息，参数：{}", id);
        return Result.success(studentService.getStudent(id));
    }
    //修改学生信息
    @PutMapping
    public Result updateStudent(@RequestBody Student student) {
        log.info("修改学生信息，参数：{}", student);
        return studentService.updateStudent(student) !=0 ? Result.success() : Result.error("修改失败");
    }
    //违纪处理
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Short score) {
        log.info("违纪处理，参数：id:{} ,score{}", id, score);
        return studentService.handleStudentViolation(id, score) !=0 ? Result.success() : Result.error("处理失败");
    }

}
